package com.higgins.client.projectmanagement;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FollowUpTab extends ScrollPanel	{
	private final ProjectServiceAsync projectService = GWT
				.create(ProjectService.class);
	private VerticalPanel verticalPanel = new VerticalPanel();
	private Button button = new Button("Refresh");
	private boolean firstRun = true;
	
	public FollowUpTab()	{
		add(verticalPanel);
		setHeight("50em");
		setWidth("100em");
	}
	public void update()	{
		if (firstRun)	{
			addHandler();
			executeFollowUpTab();
		}
		firstRun = false;
	}
	
	private void refresh()	{
		clearTab();
		executeFollowUpTab();
	}
	private void clearTab()	{
		verticalPanel.clear();
	}
	
	private void executeFollowUpTab()	{
		projectService.retrieveFollowUp(new AsyncCallback<List<String>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(List<String> results) {
				populateFollowUpTab(results);
			}
		});		
	}
	private void populateFollowUpTab(List<String> results)	{
		verticalPanel.add(button);
		verticalPanel.setCellHeight(button, Integer.toString(button.getElement().getOffsetHeight())+"px");
		for (String result:results)	{
			Label label = new Label(result);
			verticalPanel.add(label);
			verticalPanel.setCellHeight(label, Integer.toString(label.getElement().getOffsetHeight())+"px");
		}
		verticalPanel.setHeight("100%");
//		setHeight("100%");
	}

	private void addHandler()	{
		class MyHandler implements ClickHandler {
			@Override
			public void onClick(ClickEvent event) {
				refresh();
			}
		}
		MyHandler myHandler = new MyHandler();
		button.addClickHandler(myHandler);
	}
	
	
}
